package uk.ac.lkl.common.ui;

import java.awt.Component;
import java.awt.Container;

import java.awt.BorderLayout;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.logging.Logger;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.BoxLayout;

import javax.swing.event.HyperlinkListener;
import javax.swing.event.HyperlinkEvent;

import org.jdesktop.jdic.desktop.Desktop;


/**
 * A dialog for displaying a text message. The text can be html.
 * @author yish
 *
 */
public class SimpleMsgDialog extends JDialog {
	private static final Logger logger = Logger.getLogger("uk.ac.lkl");

	/**
	 * 
	 */
	private static final long serialVersionUID = -5067448276781885691L;
	
	JEditorPane  body = new JEditorPane();
	JButton done = new JButton("done");
	
	/**
	 * anchor to this component
	 */
	Component anchor;

	/** 
	 * can't be true if allwaysOnTop.
	 */
	boolean goAwayOnLostFocus = true;
	boolean allwaysOnTop      = false;
	
	public SimpleMsgDialog() {		
		Container c = getContentPane(); 
		c.setLayout(new BorderLayout());
		body.setEditable(false);
		body.setContentType("text/html");
		body.addHyperlinkListener(new HyperlinkListener(){

			public void hyperlinkUpdate(HyperlinkEvent e) {
				try {
					Desktop.browse(e.getURL());					
				} catch (Exception ex){
					logger.severe("failed to launch URL "+e+": "+ex);
				}
			}
		});
		JScrollPane sp = new JScrollPane(body);
		c.add(sp,BorderLayout.CENTER);
		
		
		// add 'done' button
		Box b = new Box(BoxLayout.X_AXIS);
		b.add(Box.createGlue());
		b.add(done);
		b.add(Box.createHorizontalStrut(20));
		c.add(b,BorderLayout.SOUTH);
		done.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e){
				setVisible(false);
			}
				
		});
		
        addWindowFocusListener(new WindowFocusListener() {
            public void windowLostFocus(WindowEvent e) {
                lostFocus();
            }
            public void windowGainedFocus(WindowEvent e) {
            }
        });
        pack();	
        setSize(300,400);
	}
	
	public SimpleMsgDialog(Component c, String title, String text) {
		this(c,text);
		setTitle(title);
	}
	
	public SimpleMsgDialog(Component c, String text) {
		this();
		setAnchor(c);
		setText(text);
	}	

	public SimpleMsgDialog(String title, String text) {
		this();
		setText(text);
		setTitle(title);
	}	

	public SimpleMsgDialog(String text) {
		this();
		setText(text);
	}
	public void setText(String text) {
		body.setText(text);
		pack();
	}
	
	/**
	 * set the text content mime type. default is text/html.
	 * @param type
	 */
	public void setMimeType(String type){
		body.setContentType(type);
	}
	
	public String getMimeType (){
		return body.getContentType();
	}
	
	public String getText() {
		return body.getText();
	}

	public SimpleMsgDialog(Component c) {
		setAnchor(c);
	}
	
	public void setAnchor(Component c) {
		anchor = c;
	}
	
	public void setVisible(boolean v){
		if (v && anchor != null) {
			setLocationRelativeTo(anchor);
		}
		super.setVisible(v);
	}
	
	public void setAllwaysOnTop(boolean ontop){
		if (ontop) goAwayOnLostFocus = false;
		setAllwaysOnTop(ontop);
	}
	
	private void lostFocus(){
		if (goAwayOnLostFocus) setVisible(false);
	}
	
	public static void main(String[] args){
		(new SimpleMsgDialog("test","hello you")).setVisible(true);
	}
}
